#include <sys/types.h> 
#include <sys/socket.h>
#include "../Include/InetListenSocket.h"

CInetListenSocket::CInetListenSocket()
{
}

CInetListenSocket::CInetListenSocket(Int32 nFD) : CTCPSocket(nFD)
{
}

CInetListenSocket::~CInetListenSocket()
{
}

Bool CInetListenSocket::Listen(Int32 nBacklogSize)
{
	return listen(m_nFD, nBacklogSize) == 0;
}

Int32 CInetListenSocket::Accept()
{
    struct sockaddr_in6 clientAddr;
#ifdef OS_WINDOWS
	Int32 nLen = sizeof(struct sockaddr_in6);
#else
	socklen_t nLen = sizeof(struct sockaddr_in6);
#endif
	return accept(m_nFD, (struct sockaddr *)&clientAddr, &nLen);
}

Int32 CInetListenSocket::Accept(CSocketAddr &addr)
{
	struct sockaddr_in6 clientAddr;
#ifdef OS_WINDOWS
	Int32 nLen = sizeof(struct sockaddr_in6);
#else
	socklen_t nLen = sizeof(struct sockaddr_in6);
#endif
	Int32 nFD = accept(m_nFD, (struct sockaddr *)&clientAddr, &nLen);

    if (nLen == sizeof(struct sockaddr_in))
    {
        addr.SetAddress(*((struct sockaddr_in *)&clientAddr));
    }
    else if (nLen == sizeof(struct sockaddr_in6))
    {
        addr.SetAddress(clientAddr);
    }

	return nFD;
}
